package com.situ.blog.dao.impl;


import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.util.JDBCUtils2;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl2 implements IBlogDao {

    @Override
    public List<Blog> selectByPage(BlogQuery blogQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try {
            connection = JDBCUtils2.getConnection();
            //  String sql = "select id,name,address,time from blog where name like ? and address like ? and time like ? limit ?,?";
            //  String sql = "select id,name,address,time from blog where name like ? and time like ? limit ?,?";
            //  String sql = "select id,name,address,time from blog where address like ? and time like ? limit ?,?";
            //  String sql = "select id,name,address,time from blog where time like ? limit ?,?";
            //  String sql = "select id,name,address,time from blog where name like ?  limit ?,?";
            //  思路：这个条件不是空，我就拼接上这个条件 and xxx like ?
            //  String sql = "select id,name,address,time from blog where 1=1 ";
            //  if (name is not null) {
            //      sql += " and name like ?"
            //  }
            //  if (address is not null) {
            //      sql += " and address like ?"
            //  }
            //  if (time is not null) {
            //      sql += " and time like ?"
            //  }
            // sql += " limit ?,?";
            String sql = "select * from blog where 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = blogQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += " and name like ?";
                queryList.add("%" + queryName + "%");
            }
            sql += " limit ?,?";
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
            statement.setInt(queryList.size() + 1, offset);
            statement.setInt(queryList.size() + 2, blogQuery.getLimit());
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("is_deleted");
                Date createTime = resultSet.getDate("create_time");
                Date updateTime = resultSet.getDate("update_time");
                Blog blog = new Blog(id, name, image, clickCount, content, typeId, status, deleted, createTime, updateTime);
                list.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public int selectTotalCount(BlogQuery blogQuery) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            connection = JDBCUtils2.getConnection();
            String sql = "select count(*) from blog where 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = blogQuery.getName();
            if (queryName != null && !queryName.equals("")) {
                sql += " and name like ?";
                queryList.add("%" + queryName + "%");
            }
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                statement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        };

        return count;
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtils2.getConnection();
            String sql = "delete from blog where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils2.close(connection, statement, null);
        }
    }

    @Override
    public void add(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtils2.getConnection();
            String sql = "insert into blog(name,content,image) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getName());
            statement.setString(2, blog.getContent());
            statement.setString(3, blog.getImage());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils2.close(connection, statement, null);
        }
    }

    @Override
    public Blog selectById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Blog blog = null;
        try {
            connection = JDBCUtils2.getConnection();
            String sql = "select * from blog where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                String name = resultSet.getString("name");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("is_deleted");
                Date createTime = resultSet.getDate("create_time");
                Date updateTime = resultSet.getDate("update_time");
                blog = new Blog(id, name, image, clickCount, content, typeId, status, deleted, createTime, updateTime);
            }
        }  catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils2.close(connection, statement, resultSet);
        }
        return blog;
    }

    @Override
    public void update(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtils2.getConnection();
            String sql = "update blog set name=?,content=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getName());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getId());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils2.close(connection, statement, null);
        }
    }
}
